#!/usr/bin/python
# -*- coding: UTF-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import os
import cv2
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt_h
from tkinter import ttk, Tk, Canvas, TclError
import tkinter as tk
from tkinter import messagebox



class Hist_window(ttk.Frame):
    def __init__(self, mainframe, image_cv, win_title_str):
        ttk.Frame.__init__(self, master=mainframe)
        self.master.geometry('600x400')
        self.master.title(win_title_str)
        self.image_cv = np.array(image_cv)
        self.show_hist()

    def show_hist(self):
        histr = cv2.calcHist([self.image_cv], [0], None, [255], [0, 255])
        # 显示中文(使用指定的汉字字体类型（此处为黑体）)
        plt_h.rcParams['font.sans-serif'] = ['SimHei']
        ##画布大小及分辨率
        figure_hist = plt_h.figure(figsize=(6, 4), dpi=100)
        ax_hist = figure_hist.add_subplot(111)
        plt_h.plot(histr)
        plt_h.grid()
        # 绘制画布
        canvas_hist = FigureCanvasTkAgg(figure_hist, master=self.master)
        canvas_hist.draw()
        canvas_hist.get_tk_widget().grid()
        ax_hist.set_xlabel('灰度值')
        ax_hist.set_ylabel('像素数量')
        ax_hist.set_title('直方图')

    def close_win(self):
        if messagebox.askokcancel("Message", "是否要关闭当前窗口?"):
            self.master.destroy()


# if __name__ == '__main__':
#     path = r'C:\Users\admin\Desktop\dt\test.bmp'  # place path to your image here
#     image_cv = cv2.imread(path, -1)
#     root = tk.Tk()
#     Hist_window(root, image_cv, "Histogram")
#     root.mainloop()
